home *** CD-ROM | disk | FTP | other *** search
- /*
- * @(#)SocketInputStream.java 1.9 95/08/19 Jonathan Payne
- *
- * Copyright (c) 1994 Sun Microsystems, Inc. All Rights Reserved.
- *
- * Permission to use, copy, modify, and distribute this software
- * and its documentation for NON-COMMERCIAL purposes and without
- * fee is hereby granted provided that this copyright notice
- * appears in all copies. Please refer to the file "copyright.html"
- * for further important copyright and licensing information.
- *
- * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
- * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
- * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
- * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR
- * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
- * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
- */
-
- package java.net;
-
- import java.io.IOException;
- import java.io.FileInputStream;
-
- /**
- * This stream extends FileInputStream to implement a
- * SocketInputStream. Note that this class should <b>NOT</b> be
- * public.
- *
- * @version 1.9, 08/19/95
- * @author Jonathan Payne
- * @author Arthur van Hoff
- */
- class SocketInputStream extends FileInputStream
- {
- private boolean eof;
- private SocketImpl impl;
- private byte temp[] = new byte[1];
-
- /**
- * Creates a new SocketInputStream. Can only be called
- * by a Socket. This method needs to hang on to the owner Socket so
- * that the fd will not be closed.
- * @param impl the implemented socket input stream
- */
- SocketInputStream(SocketImpl impl) throws IOException {
- super(impl.fd - 1);
- this.impl = impl;
- }
-
- /**
- * Reads into an array of bytes at the specified offset using
- * the received socket primitive.
- * @param b the buffer into which the data is read
- * @param off the start offset of the data
- * @param len the maximum number of bytes read
- * @return the actual number of bytes read, -1 is
- * returned when the end of the stream is reached.
- * @exception IOException If an I/O error has occurred.
- */
- private native int socketRead(byte b[], int off, int len)
- throws IOException;
-
- /**
- * Reads into a byte array data from the socket.
- * @param b the buffer into which the data is read
- * @return the actual number of bytes read, -1 is
- * returned when the end of the stream is reached.
- * @exception IOException If an I/O error has occurred.
- */
- public int read(byte b[]) throws IOException {
- return read(b, 0, b.length);
- }
-
- /**
- * Reads into a byte array <i>b</i> at offset <i>off</i>,
- * <i>length</i> bytes of data.
- * @param b the buffer into which the data is read
- * @param off the start offset of the data
- * @param len the maximum number of bytes read
- * @return the actual number of bytes read, -1 is
- * returned when the end of the stream is reached.
- * @exception IOException If an I/O error has occurred.
- */
- public int read(byte b[], int off, int length) throws IOException {
- if (eof) {
- return -1;
- }
- int n = socketRead(b, off, length);
- if (n <= 0) {
- eof = true;
- return -1;
- }
- return n;
- }
-
- /**
- * Reads a single byte from the socket.
- */
- public int read() throws IOException {
- if (eof) {
- return -1;
- }
-
- int n = read(temp, 0, 1);
- if (n <= 0) {
- return -1;
- }
- return temp[0] & 0xff;
- }
-
- /**
- * Not implemented for sockets, always returns zero.
- * @param n the number of bytes to skip
- */
- public int skip(int n) throws IOException {
- byte data[] = new byte[n];
- while (n > 0) {
- n -= read(data, 0, n);
- }
- return 0;
- }
-
- /**
- * Closes the stream.
- */
- public void close() throws IOException {
- impl.close();
- }
-
- /**
- * Overrides finalize, the fd is closed by the Socket.
- */
- protected void finalize() {}
- }
-
-